/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Console.java
 *
 * Created on Apr 16, 2011, 3:30:06 AM
 */

package newtexteditor;

import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

public class Console extends javax.swing.JFrame {

    /** Creates new form Console */
    public Console() {
        initComponents();
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jTextPane2.setEditable(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextPane2 = new javax.swing.JTextPane();

        jScrollPane1.setViewportView(jTextPane1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextPane2.setBackground(new java.awt.Color(0, 0, 0));
        jTextPane2.setForeground(new java.awt.Color(255, 255, 255));
        jTextPane2.setCaretColor(new java.awt.Color(255, 255, 255));
        jTextPane2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextPane2KeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextPane2KeyTyped(evt);
            }
        });
        jScrollPane2.setViewportView(jTextPane2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    int index = 0;
    private void jTextPane2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextPane2KeyPressed
        if(IOStream.waitingForCin == true)
        {
            if(evt.getKeyChar() == '\b' && jTextPane2.getCaretPosition() <= index)
            {
                Character temp = jTextPane2.getText().charAt(jTextPane2.getText().length()-1);
                setConsoleText(temp.toString());
            }
            if(evt.getKeyChar() == '\n')
            {
                try {
                    IOStream.setCinValue(jTextPane2.getText());
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(Console.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }//GEN-LAST:event_jTextPane2KeyPressed

    private void jTextPane2KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextPane2KeyTyped
        if(evt.getKeyChar() == '\n')
        {
            if(isNotFirstTime)
            {
                jTextPane2.select(jTextPane2.getCaretPosition()-1,jTextPane2.getCaretPosition());
                jTextPane2.replaceSelection("");
                jTextPane2.setCaretPosition(jTextPane2.getCaretPosition());
            }
        }
    }//GEN-LAST:event_jTextPane2KeyTyped
    boolean isNotFirstTime = false;
    public void setConsoleTextIndex(int index)
    {
        this.index = index;
    }
    public void setConsoleText(String toPrint)
    {
        jTextPane2.setText(jTextPane2.getText() + toPrint);
        isNotFirstTime = true;
    }
    public String getConsoleText()
    {
        return jTextPane2.getText();
    }
    public void setConsoleEditable(boolean TrueOrFalse)
    {
        jTextPane2.setEditable(TrueOrFalse);
    }
    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new Console().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JTextPane jTextPane2;
    // End of variables declaration//GEN-END:variables

}
